function [newSeqString newSeq] = addZerosAnd3Targets(baseSeq)

numLabels = 2;
numTargets = 3;
SOA = 1600;
TRDuration = 3000;

seqLength = length(baseSeq);
wordLength = log2(seqLength)/log2(numLabels); % logB(x) = log2(x)/log2(B), in case numLabels ~= 2
refreshBlockBlankLen = wordLength + 4;
refreshBlockTargetLen = wordLength + 2;

numSyncTrials = lcm(SOA, TRDuration) / SOA; % Number of trials required to sync scanner-experiment

numTrials = seqLength + numTargets*(refreshBlockTargetLen+1) + 2*(numSyncTrials+refreshBlockBlankLen) + numSyncTrials;


%% Create a string to design the sequence

newSeqString = blanks(numTrials);
newSeqString(newSeqString==' ') = '-';
newSeqString = reshape(newSeqString,numSyncTrials,numTrials/numSyncTrials);


%% Add blank blocks

blankBlock = [];
for i=1:numSyncTrials
    blankBlock = [blankBlock; 'B'];
end

refreshBlockBlank = [];
for i=1:refreshBlockBlankLen
    refreshBlockBlank = [refreshBlockBlank; 'O'];
end

newSeqString(:,1) = blankBlock;
newSeqString(1:refreshBlockBlankLen,2) = refreshBlockBlank;

newSeqString(:,8) = blankBlock;
newSeqString(1:refreshBlockBlankLen,9) = refreshBlockBlank;

newSeqString(:,end) = blankBlock;


%% Add target trials

candidateTrials = find(newSeqString=='-');

% Cut out the first 10 and the last 20
candidateTrials = candidateTrials(10:(end-20));
% Cut out the last 10 before the middle blank block
candidateTrials = candidateTrials(~ismember(candidateTrials,((7*15-9):(7*15))));

% Now, pick 4 that are spaced apart at least by 10 elements, and that is
% not more than 20 elements before the end
targetPositions = ones(1,numTargets);
while (min(targetPositions(2:end)-targetPositions(1:end-1)) < 10) || (max(targetPositions) < (max(candidateTrials)-10))
    targetPositions = candidateTrials(sort(ceil(rand(1,numTargets)*length(candidateTrials))));
end

targetBlock = 'T';
for i=1:refreshBlockTargetLen
    targetBlock = [targetBlock 'O'];
end

for i=1:length(targetPositions)
    newSeqString(targetPositions(i):(targetPositions(i)+refreshBlockTargetLen)) = targetBlock;
end



%% Now, create the real sequence
newSeq = nan(size(newSeqString));

trialIndex = 1;

% Add valid trials
for i=1:length(newSeq(:))
    if newSeqString(i) == '-'
        newSeq(i) = baseSeq(trialIndex);
        trialIndex = trialIndex + 1;
    end
end

% Add overlap trials
copyingOverlapStarted = 0;
j = nan;
for i=1:length(newSeq(:))
    if ((newSeqString(i) == 'O') && (copyingOverlapStarted == 0))
        copyingOverlapStarted = 1;
        j=i;
        % Find the last valid trial
        while newSeqString(j) ~= '-'
            if j > 1
                j = j - 1;
            else
                j = length(newSeq(:));
            end 
        end
        if newSeqString(i-1) == 'T'
            j = j - (refreshBlockTargetLen-1);
        elseif newSeqString(i-1) == 'B'
            j = j - (refreshBlockBlankLen-1);
        end
        
        newSeq(i) = newSeq(j);
        j = j + 1;
    elseif ((newSeqString(i) == 'O') && (copyingOverlapStarted == 1))
        newSeq(i) = newSeq(j);
        j = j + 1;
    else
        copyingOverlapStarted = 0;
        j = nan;
    end
end

for i=1:length(newSeq(:))
    switch newSeqString(i)
        case 'B'
            newSeq(i) = 0;
        case 'T'
            newSeq(i) = -1;
    end
end
